# coding=utf-8

import smtplib
import urllib,time
from email.mime.text import MIMEText
from email.MIMEMultipart import MIMEMultipart


def sendmail(content,sub='',to='guoyueyue<guoyueyue@baidu.com>'):
    '''send mail via GAE'''
    param=urllib.urlencode({'email_text':content, 'email_address': to, 'email_subject':sub})
    url='http://yjx527.appspot.com/sendmail'
    return urllib.urlopen(url,param).read()
    


def sendsimplemail (text, sub='', to=['guoyueyue<guoyueyue@baidu.com>',], cc=[], bcc=['yjx527@gmail.com',], \
                    fro='ye_jinxin@126.com', server='smtp.126.com', password='qf!!7jxbl'):
    import string
    msg = MIMEText(text,'html','utf-8')
    total_addrs = []
    for i in to:
        total_addrs.append(i)
    for i in cc:
        total_addrs.append(i)
    for i in bcc:
        total_addrs.append(i)
    strto = string.join(to, ",")
    strcc = string.join(cc, ",")
    strbcc = string.join(bcc, ",")
    
    msg['Subject'] = sub
    msg['From'] = fro
    msg['To'] = strto
    msg['Cc'] = strcc
    msg['Bcc'] = strbcc
    
    smtp = smtplib.SMTP()
    smtp.connect(server)
    smtp.ehlo()##SSL
    smtp.starttls() # <------ 这行就是新加的支持安全邮件的代码！
    smtp.ehlo()##
    smtp.login(fro.split('@')[0], password)
    smtp.sendmail(fro, total_addrs, msg.as_string())
    smtp.close()


def sendattachmail ():
    msg = MIMEMultipart()
    att = MIMEText(open(r'd:\aaa.txt', 'rb').read(), 'base64', 'gb2312')
    att['content-type'] = 'application/octet-stream'
    att['content-disposition'] = 'attachment;filename="aaa.txt"'
    msg.attach(att)

    content = '这是中文内容'
    body = MIMEText(content)
    msg.attach(body)

    msg['to'] = 'yjx27@qq.com'
    msg['from'] = 'yjx27@qq.com'
    msg['subject'] = 'test attachment mail'

    mailuser = 'yjx27'
    mailpwd = 'gz17sz71wh212'

    smtp = smtplib.SMTP('smtp.qq.com')
    smtp.ehlo()##SSL
    smtp.starttls() # <------ 这行就是新加的支持安全邮件的代码！
    smtp.ehlo()##
    smtp.login(mailuser, mailpwd)
    smtp.sendmail(msg['from'], msg['to'], msg.as_string())
    smtp.close()

   

if __name__ == '__main__':
##    sendsimplemail('test mail',['yejinxin@baidu.com'])
##    sendattachmail()
    content='''<font size= 5 color="blue">每次提交触发构建（包括buildonly和quick）</font><br>
<font size= 2>当构建状态为成功（buildonly和quick两个都蓝灯）时，才可提交自己的代码</font><br>
<font size= 2 color="red">提交代码后构建若失败，提交人点击这个失败构建，添加说明：失败原因+who正在跟进。</font><br>
<font size= 2 color="red">提交人负责修复失败构建，半小时内解决不了请rollback</font><br>
<font size= 2>每日23:00时，自动上传当前最新构建至<a href=http://tc-bar-safe03.tc.baidu.com:8080/browser-client/dailybuild/>dailybuild</a>，供PM体验和QA验证</font><br><br>

<font size= 5 color="blue">提测版本生成流程（由RD负责人操作）</font><br>
1.确定哪次构建作为提测版本<br>
2.编辑设置->Promotion Process->Name为提测版本号<br>
3.点击此构建->Promotion Status->找到相应的提测版本号->Force Promotion。
   Hudson自动发信通知QA提测.<br>
4.根据此次构建的revision号，在svn上打上相应的tag<br>'''

    sub='温馨提示'
    to='yejinxin<yejinxin@baidu.com>'
    to2='guoyueyue<guoyueyue@baidu.com>'
    
    sendsimplemail(content,sub)
